package com.telchina.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.domain.WoAttachment;
import com.telchina.common.mapper.WoAttachmentMapper;
import com.telchina.common.service.IWoAttachmentService;
import com.telchina.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 附件管理Service业务层处理
 *
 * @author konghao
 * @date 2023-02-21
 */
@Service
public class WoAttachmentServiceImpl extends ServiceImpl<WoAttachmentMapper, WoAttachment> implements IWoAttachmentService {

    @Autowired
    private WoAttachmentMapper woAttachmentMapper;

    /**
     * 查询附件管理
     *
     * @param id 附件管理ID
     * @return 附件管理
     */
    @Override
    public WoAttachment selectById(String id) {
        return woAttachmentMapper.selectById(id);
    }

    /**
     * 分页查询附件管理列表
     *
     * @param woAttachment 附件管理
     * @return 附件管理
     */
    @Override
    public IPage<WoAttachment> selectList(IPage<WoAttachment> page, WoAttachment woAttachment) {
        return woAttachmentMapper.selectWoAttachmentPage(page, woAttachment);
    }

    /**
     * 查询所有附件管理列表
     *
     * @param woAttachment 附件管理
     * @return 附件管理
     */
    @Override
    public List<WoAttachment> selectListAll(WoAttachment woAttachment) {
        List<WoAttachment> woAttachments = woAttachmentMapper.selectWoAttachmentList(woAttachment);
        woAttachments.stream().forEach(A -> {
            A.setFullPath("");
        });
        return woAttachments;
    }

    /**
     * 新增附件管理
     *
     * @param woAttachments 附件管理
     * @return 结果  TODO 异常应该删除文件重新上传
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(List<WoAttachment> woAttachments) {
        AtomicInteger addSum = new AtomicInteger();
        woAttachments.stream().forEach(A -> {
            A.setCreateTime(DateUtils.getNowDate());
            addSum.addAndGet(woAttachmentMapper.insert(A));
        });
        return addSum.get();
    }

    /**
     * 修改附件管理
     *
     * @param woAttachment 附件管理
     * @return 结果
     */
    @Override
    public int update(WoAttachment woAttachment) {
        woAttachment.setUpdateTime(DateUtils.getNowDate());
        return woAttachmentMapper.updateById(woAttachment);
    }

    /**
     * 批量删除附件管理
     *
     * @param ids 需要删除的附件管理ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteByIds(String[] ids) {
        return woAttachmentMapper.delete(new QueryWrapper<WoAttachment>().in("id", ids)
                .or().in("uid",ids)) > 0 ? AjaxResult.success() : AjaxResult.success();
    }

    /**
     * 删除附件管理信息
     *
     * @param id 附件管理ID
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return woAttachmentMapper.deleteById(id);
    }
}
